package cn.edu.xmu.oomall.payment.service;

import cn.edu.xmu.oomall.core.util.ReturnNo;
import cn.edu.xmu.oomall.core.util.ReturnObject;
import cn.edu.xmu.oomall.payment.dao.ShopPaymentDao;
import cn.edu.xmu.oomall.payment.model.bo.ErrorAccount;
import cn.edu.xmu.oomall.payment.model.bo.Payment;
import cn.edu.xmu.oomall.payment.model.bo.Refund;
import cn.edu.xmu.oomall.payment.model.po.ErrorAccountPo;
import cn.edu.xmu.oomall.payment.model.po.PaymentPo;
import cn.edu.xmu.oomall.payment.model.po.RefundPo;
import cn.edu.xmu.oomall.payment.model.vo.*;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.ZonedDateTime;
import java.util.ArrayList;
import java.util.List;

import static cn.edu.xmu.privilegegateway.annotation.util.Common.cloneVo;
import static cn.edu.xmu.privilegegateway.annotation.util.Common.setPoModifiedFields;

/**
 * @author 张浩山
 */
@Service
public class ShopPaymentService {
    @Autowired
    private ShopPaymentDao shopPaymentDao;

    public ReturnObject selectPayment(Integer page, Integer pageSize, String documentId, Byte state, ZonedDateTime beginTime, ZonedDateTime endTime) {
        return shopPaymentDao.selectPayment(page, pageSize, documentId, state, beginTime, endTime);
    }

    public ReturnObject selectPaymentDetail(Long id) {
        ReturnObject ret = shopPaymentDao.selectPaymentDetail(id);
        if (ret.getCode().equals(ReturnNo.OK)) {
            Payment data = (Payment)ret.getData();
            return new ReturnObject(new ShopPaymentDetailRetVo(data));
        }
        return ret;
    }

    public ReturnObject updatePayment(Long id, ShopPaymentVo paymentVo, Long loginUser, String loginUsername) {
        Payment payment = new Payment();
        payment.setId(id);
        payment.setState(paymentVo.getState());
        payment.setDescr(paymentVo.getDescr());
        setPoModifiedFields(payment, loginUser, loginUsername);
        ReturnObject ret = shopPaymentDao.updatePayment(payment.getId(), payment);
        return ret;
    }

    public ReturnObject selectRefund(Integer page, Integer pageSize, String documentId, Byte state, ZonedDateTime beginTime, ZonedDateTime endTime) {
        return shopPaymentDao.selectRefund(page, pageSize, documentId, state, beginTime, endTime);
    }

    public ReturnObject selectRefundDetail(Long id) {
        ReturnObject ret = shopPaymentDao.selectRefundDetail(id);
        if (ret.getCode().equals(ReturnNo.OK)) {
            Refund data = (Refund)ret.getData();
            return new ReturnObject(new ShopRefundDetailRetVo(data));
        }
        return ret;
    }

    public ReturnObject updateRefund(Long id, ShopRefundVo refundVo, Long loginUser, String loginUsername) {
        Refund refund = new Refund();
        refund.setId(id);
        refund.setState(refundVo.getState());
        refund.setDescr(refundVo.getDescr());
        setPoModifiedFields(refund, loginUser, loginUsername);
        ReturnObject ret = shopPaymentDao.updateRefund(refund.getId(), refund);
        return ret;
    }

    public ReturnObject selectErrorAccount(Integer page, Integer pageSize, String documentId, Byte state, ZonedDateTime beginTime, ZonedDateTime endTime) {
        return shopPaymentDao.selectErrorAccount(page, pageSize, documentId, state, beginTime, endTime);
    }

    public ReturnObject selectErrorAccountDetail(Long id) {
        ReturnObject ret = shopPaymentDao.selectErrorAccountDetail(id);
        if (ret.getCode().equals(ReturnNo.OK)) {
            ErrorAccount data = (ErrorAccount)ret.getData();
            return new ReturnObject(new ShopErrorAccountDetailRetVo(data));
        }
        return ret;
    }

    public ReturnObject updateErrorAccount(Long id, ShopErrorAccountVo errorAccountVo, Long loginUser, String loginUsername) {
        ErrorAccount errorAccount = new ErrorAccount();
        errorAccount.setId(id);
        errorAccount.setState(errorAccountVo.getState());
        errorAccount.setDescr(errorAccountVo.getDescr());
        setPoModifiedFields(errorAccount, loginUser, loginUsername);
        ReturnObject ret = shopPaymentDao.updateErrorAccount(errorAccount.getId(), errorAccount);
        return ret;
    }
}
